<?php

namespace App\User\Http\Controller\Wap;

use App\ApiBaseController;
use App\User\Models\Patients;
use Illuminate\Http\Request;
use App\User\Services\PatientsService;
use Illuminate\Support\Facades\Validator;
use App\Common\Services\UserTokenService;

class PatientsController extends ApiBaseController
{
    /**
     * 表单验证
     * @param $data
     * @param $type
     * @return bool
     * @throws GoodsException
     */
    protected function validate($data, $ruleArr, $source = '')
    {
        // 证件类型
        $identityArr = [Patients::IDENTITY_CARD];

        $rule = [
            'id' => 'required|integer|min:1',
            'u_id' => 'required|integer|min:1',
            'a_id' => 'required|integer|min:1',
            'real_name' => ['required', 'regex:/^[\x{4e00}-\x{9fa5}]+[·•]?[\x{4e00}-\x{9fa5}]+$/u', 'between:2,8'],
            'relation' => 'required|integer|between:'.Patients::RELATION_SELF.','.Patients::RELATION_FRIEND,
            'phone' => ['required', 'regex:/^((\(\d{2,3}\))|(\d{3}\-))?1(3|5|8|9)\d{9}$/'],
            'code' => 'required|integer',
            'identity_type' => 'required|in:' . implode(',', $identityArr),
            'id_card' => ['required', 'regex:/^(\d{18}|\d{17}x)$/'],
            'is_default' => 'required|integer|between:'.Patients::DEFAULT_OFF.','.Patients::DEFAULT_ON,
            'patient_id' => 'required|integer'
        ];

        // 根据场景做限制
        switch ($source) {
            case 'getData':
                $rule['id'] = 'nullable|integer|min:1';
                $rule['is_default'] = 'nullable|integer|between:'.Patients::DEFAULT_OFF.','.Patients::DEFAULT_ON;
                break;
        }

        $message = [
            'id.required' => '请传入就诊人ID',
            'id.integer' => '就诊人ID必须为整数',
            'id.min' => '就诊人ID必须大于0',
            'u_id.required' => '请传入用户ID',
            'u_id.integer' => '用户ID必须为整数',
            'u_id.min' => '用户ID必须大于0',
            'a_id.required' => '请传入认证ID',
            'a_id.integer' => '认证ID必须为整数',
            'a_id.min' => '认证ID必须大于0',
            'code.required' => '请输入验证码',
            'code.integer' => '验证码类型必须为数字',
            'registration_num.required' => '请选择号源码',
            'registration_num.regex' => '号源码格式异常',
            'real_name.required' => '请输入就诊人姓名',
            'real_name.regex' => '就诊人姓名格式异常',
            'real_name.between' => '就诊人姓名请限制在2-8个字符内',
            'relation.required' => '请选择就诊人关系',
            'relation.integer' => '就诊人关系参数必须为整数',
            'relation.between' => '就诊人关系参数异常',
            'phone.required' => '请输入就诊人通知手机号',
            'phone.regex' => '就诊人通知手机号格式异常',
            'id_card.required' => '请输入就诊人身份证号',
            'id_card.regex' => '就诊人身份证号参数异常',
            'identity_type.required' => '请选择证件类型',
            'identity_type.regex' => '证件类型格式异常',
            'is_default.required' => '请选择是否为默认就诊人',
            'is_default.integer' => '默认就诊人参数必须为整数',
            'is_default.between' => '默认就诊人参数异常',
            'patient_id.required' => '请传入患者id',
            'patient_id.integer' => '患者id必须为数字',

        ];

        // 根据配置选择规则组成
        $rule = array_intersect_key($rule, array_flip($ruleArr));

        $validate = Validator::make($data, $rule, $message);
        if($validate->fails()){
            throw new \Exception($validate->errors()->first());
        }
        return true;
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/24 1:48 下午
     * Describe: 设置默认就诊人
     * Version: V1.0
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setDefault(Request $request)
    {
        // 接受参数
        $params = $request->input();
        // 参数字段
        $fields = ['id', 'is_default'];

        try {
            // 校验参数
            $this->validate($params, $fields);

            $patientsService = new PatientsService();
            $patientsService->setDefault($params);
            return $this->success('设置成功');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * @Author   ZhangManMan
     * @DateTime 2021-06-07
     * @describe 增加就诊人
     * @version  V1.0
     * @param    Request     $request [description]
     */
    public function addPatients(Request $request)
    {
        // 接受参数
        $params = $request->input();
        // 参数字段
        $fields = ['relation', 'phone', 'code', 'real_name', 'id_card', 'identity_type'];

        try {
            // 校验参数
            $this->validate($params, $fields);

            $patientsService = new PatientsService();
            $patientsService->addData($params);
            return $this->success('添加成功');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * @Author   ZhangManMan
     * @DateTime 2021-06-07
     * @describe 修改就诊人
     * @version  V1.0
     * @param    Request     $request [description]
     */
    public function editPatients(Request $request)
    {
        // 接受参数
        $params = $request->input();
        // 参数字段
        $fields = ['id', 'relation', 'phone'];

        try {
            // 校验参数
            $this->validate($params, $fields);

            $patientsService = new PatientsService();
            $patientsService->editData($params);
            return $this->success('操作成功');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * @Author   ZhangManMan
     * @DateTime 2021-06-07
     * @describe 删除就诊人
     * @version  V1.0
     * @param    Request     $request [description]
     */
    public function delPatients(Request $request)
    {
        // 接受参数
        $params = $request->input();
        // 参数字段
        $fields = ['id'];

        try {
            // 校验参数
            $this->validate($params, $fields);

            $patientsService = new PatientsService();
            $patientsService->delData($params);

            return $this->success('操作成功');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * @Author   ZhangManMan
     * @DateTime 2021-06-07
     * @describe 获取就诊人列表
     * @version  V1.0
     * @param    Request     $request [description]
     */
    public function getList(Request $request)
    {
        // 接受参数
        $params = $request->input();

        $limit = $request->input('limit', 10);
        try {

            $patientsService = new PatientsService();
            $data = $patientsService->getList($params, $limit);
            return $this->success('获取成功', $data);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/9 4:43 下午
     * Describe: 获取就诊人详情
     * Version: V1.0
     * @param Request $request
     */
    public function getData(Request $request)
    {
        // 接受参数
        $params = $request->input();
        // 参数字段
        $fields = ['id', 'is_default'];

        try {
            // 校验参数
            $this->validate($params, $fields, 'getData');

            $patientsService = new PatientsService();
            $data = $patientsService->getData($params);
            return $this->success('获取成功', $data);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * User: Virus
     * DateTime: 2021/6/24 9:22
     * Describe: 我的就诊记录
     * Version: v1.0.0
     */
    public function mineRescue()
    {
        $param = request()->input();
        try {
            $rules = ['patient_id'];
            $this->validate($param, $rules);
            $service = new PatientsService();
            $res = $service->mineRescue($param);
            return $this->success('success', $res);
        }catch (\Exception $exception){
            return $this->error($exception->getMessage());
        }
    }

}
